package 程序员面试金典;

import java.util.*;

public class T9键盘 {
    //自己写的
    public List<String> getValidT9Words(String num, String[] words) {
        Map<Integer, char[]> map = new HashMap<>();
        map.put(2,new char[]{'a','b','c'});
        map.put(3,new char[]{'d','e','f'});
        map.put(4,new char[]{'g','h','i'});
        map.put(5,new char[]{'j','k','l'});
        map.put(6,new char[]{'m','n','o'});
        map.put(7,new char[]{'p','q','r','s'});
        map.put(8,new char[]{'t','u','v'});
        map.put(9,new char[]{'w','x','y','z'});
        List<String> list = new LinkedList<>();
        char[] array = num.toCharArray();
        for (String word : words) {
            int count=0;
            char[] chars = word.toCharArray();
            for (int i = 0; i < word.length(); i++) {
                char[] chars1 = map.get(array[i] - '0');
                for (int j = 0; j < chars1.length; j++) {
                    if (chars1[j]==chars[i]){
                        count++;
                        break;
                    }
                }
            }
            if (count==word.length())
                list.add(word);
        }
        return list;
    }
    //别人写的
//    public List<String> getValidT9Words(String num, String[] words) {
//        char arr[] = "22233344455566677778889999".toCharArray();
//        ArrayList<String> res = new ArrayList<>();
//        for (String word : words) {
//            boolean flag = true;
//            for (int i = 0; i < word.length(); i++) {
//                if(arr[word.charAt(i) - 'a'] != num.charAt(i)){
//                    flag = false;
//                    break;
//                }
//            }
//            if (flag) {
//                res.add(word);
//            }
//        }
//        return res;
//    }
}
